package com.boilerCloud.controller.device;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.device.ModelCode;
import com.boilerCloud.dto.sys.ReqResult;
import com.boilerCloud.service.device.IModelCodeService;


@RestController
@RequestMapping("/model/")
public class ModelCodeControler {
	private static final Logger log = LoggerFactory.getLogger(ModelCodeControler.class);
	@Resource
	private IModelCodeService service;


	@RequestMapping(value = "modelCodeList")
	public ModelAndView modelCodeList() {
		return new ModelAndView("/views/model/modelCodeInfo.html");
	}
	
	

	/**
	 * 查询模组编码列表
	 * @param grade
	 * @param yema
	 * @param length
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "qryModelCodeList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult qryModelList(@ModelAttribute ModelCode model ,
			@RequestParam(required = true) int yema,
			@RequestParam(required = true) int length,HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			
			HzlqswPage<ModelCode> page = new HzlqswPage<ModelCode>();
			page.setCurPage(yema);
			page.setPageSize(length);
			service.qryList(model,page);
			rst.setReturnObject(page);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("查询模组编码列表失败");
			log.error("查询模组编码列表异常",e);
			return rst;
		}
	}
	
	/**
	 * 修改模组编码信息
	 * @param device
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "editModelCode", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult editModelCode(
			@ModelAttribute  ModelCode model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			service.edit(model);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("修改模组编码信息失败");
			log.error("修改模组编码信息异常",e);
			return rst;
		}
	}
	
	@RequestMapping(value = "getModelCode", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getModelCode(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			 ModelCode model = service.getModelCode(id);
			rst.setResultCode(ReqResult.resultCode_Success);
			rst.setReturnObject(model);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("获取模组编码信息失败");
			log.error("获取模组编码信息异常",e);
			return rst;
		}
	}
	
	/**
	 * 新增模组编码
	 * @param device
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addModelCode", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult addModelCode(
			@ModelAttribute  ModelCode model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			service.save(model);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("新增模组编码失败");
			log.error("新增模组编码异常",e);
			return rst;
		}
	}
	
	/**
	 * 删除模组编码
	 * @param businessNo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "delModelCode", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult delModel(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			service.del(id);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("模组编码失败");
			log.error("模组编码异常",e);
			return rst;
		}
	}
	

	/**
	 * 获取模组编码列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getModelCodeList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getModelCodeList(
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			List<ModelCode> list = service.getModelCodeList(null);
			rst.setResultCode(ReqResult.resultCode_Success);
			rst.setReturnObject(list);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("模组编码失败");
			log.error("模组编码异常",e);
			return rst;
		}
	}

	

}
